﻿#define _ASYNC	// 异步多线程方式

using System;
using System.Threading;

namespace MultiThread.DosApp
{
	/// <summary>
	/// 主应用程序类
	/// </summary>
	class MainApp
	{
		/// <summary>
		/// 应用程序主函数
		/// </summary>
		/// <param name="args"></param>
		static void Main(string[] args)
		{
			// 打印提示信息, 按 ENTER 键继续
			Console.WriteLine("HINT: press 'ENTER' exit.");

			// 开始应用程序
			(new MainApp()).Start();

			// 读入回车符
			Console.ReadLine();
		}

		/// <summary>
		/// 开始应用程序
		/// </summary>
		public void Start()
		{
			String[] nameArray = new String[] {
				"FengYuan", 
				"JinHaiJiang", 
				"HanNing", 
				"SongChaoPeng", 
				"YeChen", 
			};

#if _ASYNC
			Thread[] tArray = new Thread[nameArray.Length];

			for (int i = 0; i < nameArray.Length; i++)
			{
				// 创建用户
				User user = new User(i, nameArray[i]);

				// 创建线程
				tArray[i] = new Thread(new ThreadStart(user.Thieve));
			}

			for (int j = 0; j < tArray.Length; j++)
			{
				// 执行线程
				tArray[j].Start();
			}
#else
			for (int i = 0; i < nameArray.Length; i++)
			{
				// 创建用户
				User user = new User(i, nameArray[i]);
				// 偷菜
				user.Thieve();
			}
#endif
		}
	}
}
